
package com;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.NavigableSet;
import java.util.Scanner;
import java.util.TreeSet;

public class WrongPrice {

	private static Scanner scanner;
	private static BufferedWriter out;
	
	public static void main(String[] args) {
		try {
			out = new BufferedWriter(new FileWriter("price_wrong//out.txt"));
			scanner = new Scanner(new File("price_wrong//input//B-small-practice.in"));
			int testCases = scanner.nextInt();
			scanner.nextLine();
			for (int i=0; i<testCases; i++) {
				doTestCase(i+1);
			}
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private static void doTestCase(int testCase) {
		String[] items = scanner.nextLine().split(" ");
		int[] costs = new int[items.length];
		for(int i=0; i<items.length;i++) {
			costs[i] = scanner.nextInt();
		}
		scanner.nextLine();
		
		LinkedList<Pair> list = new LinkedList<Pair>();
		TreeSet<String> allItems = new TreeSet<String>();
		
		for (int j=items.length-1;j>=0;j--) {
			for (int k=0; k<j; k++) {
				if(costs[k]> costs[k+1]) {
					int itemCost = costs[k];
					costs[k] = costs[k+1];
					costs[k+1] = itemCost;
					
					String itemName = items[k];
					items[k] = items[k+1];
					items[k+1] = itemName;
					
					list.add(new Pair(items[k], items[k+1]));
					allItems.add(items[k]);
					allItems.add(items[k+1]);
				}
			}
		}
		Pair[] itemList = list.toArray(new Pair[0]);
		boolean[] removed = new boolean[list.size()];
		int count =0;
		List<String> result = generateNewList(count, itemList, removed, allItems);
		while(result == null && count < 10)
			result = generateNewList(count++, itemList, removed, allItems);

		if (result == null)
			System.out.println("error");
		else {
		try {
			out.write("Case #" + testCase +":");
			for(String s: result)
				out.write(" "+s);
			out.write("\n");
		} catch (IOException e) {
			e.printStackTrace();
		}
		} 
	}

	private static List<String> generateNewList(int count, Pair[] list, boolean[] removed, NavigableSet<String> navigatableSet){
		if (count == 0) {
			for(boolean b: removed) {
				if(!b)
					return null;
			}
			return new LinkedList<String>(); 
		}
		for(String s: navigatableSet) {
			Pair[] newList = Arrays.copyOf(list, list.length);
			boolean[] newRemoved = Arrays.copyOf(removed, removed.length);
			boolean changed = false;
			for(int a=0; a<list.length;a++) {
				if(!removed[a] && (list[a].first == s || list[a].second == s)) {
					newRemoved[a] = true;
					changed = true;
				}
			}
			if(!changed)
				continue;
			List<String> result = generateNewList(count -1, newList, newRemoved, navigatableSet.tailSet(s, false));
			if(result != null) {
				result.add(0, s);
				return result;
			}
		}
		return null;
	}
	
	static class Pair{
		
		String first;
		String second;
		
		public Pair(String first, String second) {
			this.first = first;
			this.second = second;
		}
		
	}
}